<?php

namespace App\Console\Commands;

use App\Article;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Symfony\Component\DomCrawler\Crawler;

class FetchNewsJkmj extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'spider:jkmjnews';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '抓取即刻美剧新闻';

    protected $base_url = 'https://www.tvjike.com';

    protected $client;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        set_time_limit(1000);
        //获取登陆cookie
        $header_params = [
            'Host' => $this->base_url,
            'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'Accept-Encoding' => 'gzip, deflate, sdch',
            'Accept-Language' => 'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4',
            'Cache-control:' => 'max-age=0',
            'Dnt' => '1',
            'Referer' => 'http://cn.bing.com/search?q=%E7%BE%8E%E5%89%A7%E6%96%B0%E9%97%BB&qs=n&form=QBRE&sp=-1&pq=%E7%BE%8E%E5%89%A7%E6%96%B0%E9%97%BB&sc=8-4&sk=&cvid=1DE1DCD785DC4191AFD7BB57F8398543',
            'Upgrade-Insecure-Requests' => '1',
            'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36',
        ];
        //使用共享cookie和伪造headers
        $this->client = new Client([
            'cookies' => true,
            'headers' => $header_params,
            'base_uri' => $this->base_url,
            'http_errors' => false
        ]);
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $response = $this->client->request('GET', '/');
        if ($code = $response->getStatusCode() == 200) {
            $html = $response->getBody()->getContents();
            $crawler = new Crawler($html);
            $crawler->filter('h2.title > a')->each(function (Crawler $row, $i) {
                $href = $row->attr('href');
                //数据库没有文章 抓取文章
                $news_response = $this->client->request('GET', $href);
                //var_dump($news_response->getStatusCode());
                if ($news_response->getStatusCode() == 200) {
                    try {
                        $news_html = $news_response->getBody()->getContents();
                        $news_crawler = new Crawler($news_html);
                        $title = $news_crawler->filter('h2.title')->first()->text();
                        $imgUrls = [];
                        $news_crawler->filter('div.entry img')->each(function (Crawler $node, $idx) use (&$imgUrls) {
                            $imgUrls[] = $node->attr('src');
                        });
                        $seo_key_word = $news_crawler->filter('head > meta[name="keywords"]')->first()->attr('content');
                        $seo_description = $news_crawler->filter('head > meta[name="description"]')->first()->attr('content');
                        $body = $news_crawler->filter('div.entry')->html();
                        Article::Spider_Insert($title, $body, $href, $imgUrls)->safeInsertSEO($seo_key_word, $seo_description);
                    } catch (\Exception $exp) {
                        $message = '抓取即刻美剧失败' . $href . "\n" . $exp->getMessage() . "\n";
                        \Log::alert($message);
                        echo $message . "\n";
                    }
                }
            });
        } else {
            $msg = $this->base_url . '网页加载失败';
            \Log::info($msg);
            echo $msg . "\n";

        }
    }
}
